﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameHungTrung
{
    public class My2DSprite
    {

        Texture2D[] _Texture;

        public Texture2D[] Texture
        {
            get { return _Texture; }
            set { _Texture = value;
                    indexTexture = 0;
                    NTextures = _Texture.Length;
            }
        }

        int indexTexture;

        Vector2 _position;

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        int _nTextures;

        public int NTextures
        {
            get { return _nTextures; }
            set { _nTextures = value; }
        }              

        private float _mAlphaValue;

        public float mAlphaValue
        {
            get { return _mAlphaValue; }
            set { _mAlphaValue = value; }
        }

        private Color _color;

        public int getWidthTexture()
        {
            return Texture[indexTexture].Width;
        }

        public int getHeightTexture()
        {
            return Texture[indexTexture].Height;
        }

        public My2DSprite(Texture2D[] texture, Vector2 position)
        {
            Texture = texture;
            Position = position;
            _color = Color.White;
            mAlphaValue = 1.0f;            
        }

        public void UpdateTexture(Texture2D [] textures)
        {
            Texture = textures;
        }

        public void Update(GameTime gameTime)
        {          
            indexTexture = (indexTexture + 1) % _nTextures;
        }              

        public void UpdateColor(Color color)
        {
            _color = color;
        }

        public void UpdatePosition(Vector2 vect)
        {
            _position.X = vect.X;
            _position.Y = vect.Y;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_Texture[indexTexture], _position, Color.Lerp(Color.Transparent, _color, mAlphaValue));
        }
        
        public bool IsCollision(Vector2 anotherObject)
        {
            double x = anotherObject.X;
            double y = anotherObject.Y;
            if ((x >= _position.X && x <= (_position.X + _Texture[indexTexture].Width))
                && (y >= _position.Y && y <= (_position.Y + _Texture[indexTexture].Height)))
                return true;
            return false; 
        }

        public bool IsSelected(double x, double y)
        {
            Rectangle sourceRect = new Rectangle((int)Position.X, (int)Position.Y,(int) getWidthTexture(), (int)getHeightTexture());
            if (x < sourceRect.Left || x >= sourceRect.Left + sourceRect.Width)
                return false;
            if (y < sourceRect.Top || y >= sourceRect.Top + sourceRect.Height)
                return false;
            return true;

        }
    }
}
